home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / applic / ntp / acts.zoo / setcfg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-14  |  13.1 KB  |  471 lines

  1. void setcfg()
  2. {
  3. /*
  4.         this subroutine opens the configuration file named
  5.         nbstime.cfg and sets the various global parameters
  6.         based on the contents.  if the file does not exist
  7.         the globals are not altered and the default configuration
  8.         is used.  the global parameters are defined in the main
  9.         program preamble.
  10.     the configuration file is slight different for the different
  11.     versions of the program.  
  12.     the first line always contains the telephone number.
  13.     the second line contains the port specifier at the start.
  14.     for the ibm pc version, this is a single digit or h (for
  15.     a hardware address to be specified on line 6). for the
  16.     UNIX version, if the first two character are letters or
  17.     numbers (nn), they define the port address as /dev/tty<nn>.
  18.     if the first is a letter or number (n) and  the second is - then
  19.     the port address is /dev/tty<n>. if both positions are -, then
  20.     the port is specified by its full path on the last line.
  21. */
  22. #include "nbstime.h"
  23. #include <stdio.h>
  24. #ifdef SUN
  25. #include <sys/file.h>
  26. #endif
  27. #include <ctype.h>
  28. FILE *fopen();
  29. FILE *iop;
  30. int getlst();
  31. extern FILE *jop;
  32. extern int debug;       /* turn on debug mode if = 1 */
  33. #ifdef SUN
  34. char dv[20]; /*device path name*/
  35. #endif
  36. int j,eol,icol;
  37. char c;
  38. char *peof,*perr;
  39. /*
  40.     the IBMPC version can convert the received time
  41.     from UTC to local time using the following two flags.
  42.     the SUN version always sets the time directly in 
  43.     UTC seconds since 1970 since that is the UNIX standard
  44. */
  45. #ifdef IBMPC
  46. extern int utcdif;         /* local time - utc in hours */
  47. extern int dsflag;         /* daylight savings time? 1=yes, 0=no */
  48. #endif
  49. /*
  50.     for the IBMPC version, cmport specifies the comport, 0=com1,
  51.     1=com2, etc.  
  52.     for the SUN version, cmport is the file handle returned when
  53.     the comport is opened at the end of this routine.
  54. */
  55. extern int cmport; 
  56. #ifdef IBMPC
  57. extern int atflag;         /* 1=AT-type machine with CMOS clock, 0= not */
  58. #endif
  59. extern char number[30];    /* number to dial with trailing <cr> and 0 */
  60. extern int echo;           /* expect echoes from modem? 1=yes, 0=no */
  61. extern int hs;             /* 1 = use 1200 baud, 0=use 300 baud     */
  62. #ifdef IBMPC
  63. extern int lpt;             /* 1=  pulse lp on time, 0= do not pulse */
  64. #endif
  65. extern int setclk;         /*  1= set computer clock, 0= do not set */
  66. /*
  67.     wrtdif = 1      compute current time difference and write to file
  68.     wrtdif = 0      do not write to file
  69.     wrtdif = 2      write current difference to file and estimate rate
  70.               using second difference divided by elapsed time
  71. */
  72. extern int wrtdif; 
  73. #ifdef IBMPC
  74. #ifndef BIOS
  75. extern int cmadr;          /* hardware address of port for _d version*/
  76. #endif
  77. #endif
  78.         perr="\n Syntax error in config. file ";
  79.         peof=" -- premature EOF \n ";
  80.         iop=fopen("nbstime.cfg","rt");
  81.         if(iop == 0)
  82.         {
  83.         printf("\n can't open file nbstime.cfg, use default configuration.");
  84.         return;
  85.         }
  86. /*
  87.         first line of file is full telephone number. read it and store
  88.         after ATD which starts command to modem
  89. */
  90.         eol=0;
  91.         for(j=3; (j<29) && (eol==0); j++)
  92.         {
  93.         c=getc(iop);
  94.         switch(c)
  95.            {
  96.            case EOF:
  97.               printf("%s %s",perr,peof);
  98.               abort();
  99.               break;
  100.            case '\n':
  101.               number[j]='\r';        /*terminate with carriage return*/
  102.               number[j+1]=0;        /* and then a null */
  103.               eol=1;
  104.               break;
  105.            default:
  106.               number[j]=c;
  107.               break;
  108.            }
  109.         }
  110.         if(eol == 0)
  111.         {
  112.         printf("%s -- telephone # too long. \n",perr);
  113.         abort();
  114.         }
  115. /*
  116.     for ibm version:
  117.     next line contains comport, 1 = com1, 2 = com2 in col 1, etc.
  118.         if first line contains h, then comport hardware address will be
  119.         specified on sixth line of file.  if comport is specified by
  120.     number, sixth line of file is not used.
  121.         second char sets echo: B=0, anything else =1,
  122.         third character is baud rate, h=1200, l=300;
  123.         fourth character is line printer y=yes, n=no.
  124.         fifth character is set clocks, s=set, d=disable set.
  125.         rest of line is ignored and may be used for a comment
  126.  
  127.     for sun version:
  128.     if first two characters are alphanumeric, they specify port.
  129.     if first character is alphanumeric and second is -, first is port.
  130.     if first is - then port is specified by its full address.
  131.     note that length of port specifier may be 1 or 2 chars so that
  132.     the word next below depends on how the port was specified
  133.     next character is echo as above.
  134.     next is baud rate as above
  135.     next is set/ no set as above.
  136. */
  137.     icol=0;            /* keep track of what col. we are in*/
  138.         c=getc(iop);
  139.     icol++;
  140.         switch (c)
  141.         {
  142.         case EOF:
  143.            printf("%s %s",perr,peof);
  144.            abort();
  145.            break;
  146. #ifdef IBMPC
  147.         case '1':
  148.            cmport=0;
  149.            break;
  150.         case '2':
  151.            cmport=1;
  152.            break;
  153.         #ifndef BIOS
  154.         case '3':
  155.            cmport=2;
  156.            break;
  157.         case '4':
  158.            cmport=3;
  159.            break;
  160.         case 'h':
  161.         case 'H':
  162.            cmport= -1;
  163.            break;
  164.         #endif
  165.         default:
  166.            #ifdef BIOS
  167.            printf("%s -- comport not 1 or 2 \n",perr);
  168.        #endif
  169.        #ifndef BIOS
  170.            printf("%s -- comport must be 1,2 3,4 or h \n",perr);
  171.            #endif
  172.            abort();
  173.            break;
  174. #endif
  175. #ifdef SUN
  176.     case '-':           /* port to be specified by path name */
  177.        cmport= -1;
  178.        break;
  179.     default:
  180.        if( (isalpha(c) == 0) && (isdigit(c) == 0) )
  181.         {
  182.         printf("%s -- port address must be alphanumeric \n",perr);
  183.         abort();
  184.         break;
  185.         }
  186. /*
  187.     name begins with /dev/tty then add chars just read
  188. */
  189.        sprintf(&dv[0],"%s","/dev/tty");
  190.        dv[8]=c;
  191.        c=getc(iop);
  192.        icol++;
  193.        if(c == '-')             /* comport is only 1 char long*/
  194.         {
  195.         dv[9]='\0';         /*terminate string with null*/
  196.         cmport=1;           /*show port specified ok.   */
  197.         break;
  198.         }
  199.        if( (isalpha(c) == 0) && (isdigit(c) == 0) )
  200.         {
  201.         printf("%s -- port address must be alphanumeric \n",perr);
  202.         abort();
  203.         break;
  204.         }
  205.        dv[9]=c;            /* get second character of comport*/
  206.        dv[10]='\0';        /* and terminate with null byte */
  207.        cmport= 1;          /* show port specified ok.      */ 
  208.        break;
  209. #endif
  210.         }
  211.         c=getc(iop);
  212.     icol++;
  213.         switch(c)
  214.         {
  215.         case EOF:
  216.            printf("%s %s", perr,peof);
  217.            abort();
  218.            break;
  219.         case 'b':
  220.         case 'B':
  221.            echo=0;
  222.            break;
  223.         case 'e':
  224.         case 'E':
  225.            echo=1;
  226.            break;
  227.         default:
  228.            printf("\nWarning, Line 2 Char %d not e or b -- e assumed.",icol);
  229.            echo=1;
  230.            break;
  231.         }
  232.         c=getc(iop);
  233.     icol++;
  234.         switch (c)
  235.         {
  236.         case EOF:
  237.            printf("%s %s",perr,peof);
  238.            abort();
  239.         case 'h':
  240.         case 'H':
  241.            hs=1;
  242.            break;
  243.         case 'l':
  244.         case 'L':
  245.            hs=0;
  246.            break;
  247.         default:
  248.            printf("\n Warning, Line 2 Char %d not h or l -- h assumed.",icol);
  249.            hs=1;
  250.            break;
  251.         }
  252. #ifdef IBMPC
  253.         c=getc(iop);
  254.     icol++;
  255.         switch (c)
  256.         {
  257.         case EOF:
  258.            printf("%s %s",perr,peof);
  259.            abort();
  260.         case 'y':
  261.         case 'Y':
  262.            lpt=1;
  263.            break;
  264.         case 'n':
  265.         case 'N':
  266.            lpt=0;
  267.            break;
  268.         default:
  269.            printf("\n Warning, Line 2 Char %d not y or n -- n assumed.",icol);
  270.            lpt=0;
  271.            break;
  272.         }
  273. #endif
  274.         c=getc(iop);
  275.     icol++;
  276.         switch (c)
  277.         {
  278.         case EOF:
  279.            printf("%s %s",perr,peof);
  280.            abort();
  281.         case 's':
  282.         case 'S':
  283.            setclk=1;
  284.            break;
  285.         case 'd':
  286.         case 'D':
  287.            setclk=0;
  288.            break;
  289.         default:
  290.            printf("\n Warning, Line 2 Char %d not s or d -- d assumed.",icol);
  291.            setclk=0;
  292.            break;
  293.         }
  294.         c=getc(iop);
  295.     icol++;
  296.         switch (c)
  297.         {
  298.         case EOF:
  299.            printf("%s %s",perr,peof);
  300.            abort();
  301. /*
  302.     the next character specifies if the time difference is to be
  303.     stored in the archive file.  a response of "a" means store difference
  304.     and a response of "n" means do not store it.  any other response is
  305.     taken to be "n" and a diagnostic is printed.  In addition, an upper
  306.     case response "A" also means estimate the rate offset of the clock
  307.     by comparing the last difference with the current difference and
  308.     dividing the second difference by the elapsed time.  this value is
  309.     the rate offset with respect to UTC(NBS).
  310. */
  311.     case 'A':
  312.        if( getlst() == 0)
  313.           {
  314.           printf("\n Can't read previous difference for rate estimate.");
  315.           wrtdif=0;
  316.           }
  317.        else
  318.           {
  319.           wrtdif=2;
  320.           break;
  321.           }
  322.         case 'a':
  323.            wrtdif=1;
  324.            if(  (jop=fopen("nbstime.dif","at")) == 0)
  325.               {
  326.               printf("\n Cannot open file for writing time difference");
  327.               wrtdif=0;
  328.               }
  329.            break;
  330.         case 'n':
  331.         case 'N':
  332.            wrtdif=0;
  333.            break;
  334.         default:
  335.            printf("\n Warning, Line 2 Char %d not a or n -- n assumed.",icol);
  336.            wrtdif=0;
  337.            break;
  338.         }
  339.         while( ( (c=getc(iop)) != '\n') && (c != EOF) );/*skip rest of line*/
  340.         if(c == EOF)
  341.         {
  342.         printf("%s %s",perr,peof);
  343.         abort();
  344.         }
  345. #ifdef IBMPC
  346. /*
  347.         next line contains utcdif as number for hours or symbol for zone
  348.     note that this is not used for SUN since conversion is to UTC
  349.     directly.
  350. */
  351.         c=getc(iop);
  352.         switch (c)
  353.         {
  354.         case 'p':
  355.         case 'P':
  356.            utcdif= -8;
  357.            break;
  358.         case 'm':
  359.         case 'M':
  360.            utcdif= -7;
  361.            break;
  362.         case 'c':
  363.         case 'C':
  364.            utcdif= -6;
  365.            break;
  366.         case 'e':
  367.         case 'E':
  368.            utcdif= -5;
  369.            break;
  370.         case 'z':
  371.         case 'Z':
  372.            utcdif=0;
  373.            break;
  374.         case '-':
  375.         case '+':
  376.         case '0':
  377.         case '1':
  378.         case '2':
  379.         case '3':
  380.         case '4':
  381.         case '5':
  382.         case '6':
  383.         case '7':
  384.         case '8':
  385.         case '9':
  386.            ungetc(c,iop);
  387.            fscanf(iop,"%d",&utcdif);
  388.            break;
  389.         default:
  390.            printf("\n %s -- syntax error in Line 3 -- utc dif.\n",perr);
  391.            abort();
  392.            break;
  393.         }
  394.         while( ( (c=getc(iop) ) != '\n') && (c != EOF));/*skip to next line*/
  395.         if(c == EOF)
  396.         {
  397.         printf("%s %s",perr,peof);
  398.         abort();
  399.         }
  400.         j=fscanf(iop,"%d \n%d",&dsflag,&atflag);
  401.         if(j == EOF)
  402.         {
  403.         printf("%s %s",perr,peof);
  404.         abort();
  405.         }
  406.         if(j != 2)
  407.         {
  408.         printf("%s -- syntax error in AT flag or ds flag \n",perr);
  409.         abort();
  410.         }
  411. #endif
  412. /*
  413.         if the comport is to be specified by its hardware address,
  414.         then the first character on line 2 was 'h' for the IBMPC
  415.     version and '-' for the SUN version -- comport is now -1.
  416.         if so, read the hardware address from the next line.  if comport
  417.         is positive or zero, then next line is ignored.
  418.  
  419.     note that line is read differently depending on which version
  420.     is being generated.  for IBMPC is is read as a hex address
  421.     while for  the SUN it is read as a full path name.
  422.     
  423.     also for the IBMPC the last read was for the AT and ds flags
  424.     and the newline must be skipped at the end.  for the sun
  425.     the last read was for line 2 and the newline at the end
  426.     of line 2 has already been skipped there
  427.  
  428.     also note that the IBMPC code is only compiled if the 
  429.     non-BIOS version is being generated.  The bios version
  430.     does not used the hardware address of the port.
  431. */
  432.         if(cmport < 0)
  433.         {
  434. #ifdef IBMPC
  435. #ifndef BIOS
  436.         while( ( (c=getc(iop)) != '\n') && (c != EOF) ) ;/*skip to next line*/
  437.         if(c == EOF)
  438.            {
  439.            printf("%s %s",perr,peof);
  440.            printf("\n while trying to read comport hardware address.");
  441.            abort();
  442.            }
  443.         j=fscanf(iop,"\n%x",&cmadr);
  444. #endif
  445. #endif
  446. #ifdef SUN
  447.     j=fscanf(iop,"%s",&dv[0]);
  448. #endif
  449.         if(j < 1)
  450.            {
  451.            printf("\n Syntax error reading comport hardware address.");
  452.            abort();
  453.            }
  454.     }  
  455. #ifdef SUN
  456. /*
  457.     now try to open port using path name either read above
  458.     or constructed from template
  459. */
  460.     if(debug != 0) printf("\n port= %s",dv);
  461.     cmport=open(dv, O_RDWR | O_NDELAY);
  462.     if(debug != 0) printf("\n port file descriptor=%d",cmport);
  463.     if(cmport < 0 )
  464.        {
  465.        printf("\n error opening port %s",dv);
  466.        abort();
  467.        }
  468. #endif
  469.         fclose(iop);
  470. }
  471.